﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace LiteratureManage.Web.Helpers
{
    public static class PagerExtensions
    {
        /// <summary>
        /// 分页Pager显示
        /// </summary>
        /// <param name="html"></param>
        /// <param name="currentPageStr">标识当前页码的QueryStringKey</param>
        /// <param name="pageSize">每页显示</param>
        /// <param name="totalCount">总数据量</param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
        {
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            int currentPage = 1; //当前页
            if (!int.TryParse(queryString[currentPageStr], out currentPage)) currentPage = 1; //与相应的QueryString绑定
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var dict = new RouteValueDictionary(html.ViewContext.RouteData.Values);

            var output = new StringBuilder();

            foreach (string key in queryString.Keys)
                if (queryString[key] != null && !string.IsNullOrEmpty(key))
                    dict[key] = queryString[key];

            if (totalPages > 1)
            {
                if (currentPage != 1)
                {//处理首页连接
                    dict[currentPageStr] = 1;
                    output.AppendFormat("{0}", html.RouteLink("首页", dict));
                }
                if (currentPage > 1)
                {//处理上一页的连接
                    dict[currentPageStr] = currentPage - 1;
                    output.AppendFormat("{0}", html.RouteLink("上一页", dict));
                }
                else
                {
                    output.AppendFormat("<span class=\"nextprev\">{0}</span>", "上一页");
                }
                int currint = 5;
                for (int i = 0; i <= 10; i++)
                {//一共最多显示10个页码，前面5个，后面5个
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                        if (currint == i)
                        {//当前页处理
                            output.Append(string.Format("<span class=\"current\">{0}</span>", currentPage));
                        }
                        else
                        {//一般页处理
                            dict[currentPageStr] = currentPage + i - currint;
                            output.AppendFormat("{0}", html.RouteLink((currentPage + i - currint).ToString(), dict));
                        }
                }
                if (currentPage < totalPages)
                {//处理下一页的链接
                    dict[currentPageStr] = currentPage + 1;
                    output.AppendFormat("{0}", html.RouteLink("下一页", dict));
                }
                else
                {
                    output.AppendFormat("<span class=\"nextprev\">{0}</span>", "下一页");
                }
                if (currentPage != totalPages)
                {
                    dict[currentPageStr] = totalPages;
                    output.AppendFormat("{0}", html.RouteLink("末页", dict));
                }
            }
            output.AppendFormat("<span class=\"nextprev\">第{0}页/共{1}页</span>", currentPage, totalPages);//这个统计加不加都行
            return output.ToString();
        }
    }
}
